package com.cloud.school.app.config.security;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by enHui.Chen on 2019/1/30.
 */
@Slf4j
public class AliyunSendSms {
    private static final String regionId = "default";
    private static final String accessKeyID = "LTAIrUrgdssnXhZM";
    private static final String accessKeySecret = "zTXHnqF0AqhIgSnIor70tYtt22fb4J";
    private static final String SignName = "";
    private static final String TemplateCode = "SMS_157355822";

    public static void sendSms(String telephone, String validateCode) {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyID, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setSysProtocol(ProtocolType.HTTPS);
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", telephone);
        request.putQueryParameter("SignName", SignName);
        request.putQueryParameter("TemplateCode", TemplateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + validateCode + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info("发送短信结果:{}" + response.getData());
        } catch (ServerException e) {
            log.error("发送短信异常,原因{}", e.getMessage(), e);
        } catch (ClientException e) {
            log.error("发送短信异常,原因{}", e.getMessage(), e);
        }
    }
}
